package com.jungle.client;

import com.jungle.base.enums.AccountConfigMethod;
import com.jungle.base.enums.DebitCredit;
import com.jungle.bfi.domain.vo.Option;
import com.jungle.bfi.domain.vo.VoucherEntryTemplateVo;
import com.jungle.client.control.BasicDialog;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.function.Consumer;

import static com.jungle.client.FxApplication.bundle;

@Slf4j
public class EntryTemplateEditController extends BasicDialog<VoucherEntryTemplateVo> {
    @FXML
    private ChoiceBox<Option> configMethodField;
    @FXML
    private TextField nameField;
    @FXML
    private TextField codeField;
    @FXML
    private RadioButton creditField;
    @FXML
    private RadioButton debitField;
    private final ToggleGroup debitCreditGroup = new ToggleGroup();
    @FXML
    private TextArea summaryTemplateField;
    private String id;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        super.initialize(location, resources);
        configMethodField.getItems().add(new Option(bundle.getString("entryTemplate.configMethod.choice.assign"), AccountConfigMethod.ASSIGN.name()));
        configMethodField.getItems().add(new Option(bundle.getString("entryTemplate.configMethod.choice.mapping"), AccountConfigMethod.MAPPING.name()));

        configMethodField.getSelectionModel().select(0);

        creditField.setToggleGroup(debitCreditGroup);
        debitField.setToggleGroup(debitCreditGroup);

        creditField.setSelected(true);
    }
    @Override
    public void loadData(VoucherEntryTemplateVo data) {
        id = data.getId();
        nameField.setText(data.getAccountName());
        codeField.setText(data.getAccountCode());
        String debitOrCredit = data.getDebitOrCredit();
        creditField.setSelected(DebitCredit.CREDIT.name().equals(debitOrCredit));
        debitField.setSelected(DebitCredit.DEBIT.name().equals(debitOrCredit));
        summaryTemplateField.setText(data.getSummaryTemplate());
    }

    @Override
    public VoucherEntryTemplateVo storeData() {
        VoucherEntryTemplateVo data = new VoucherEntryTemplateVo();
        data.setId(id);
        data.setAccountName(nameField.getText());
        data.setAccountCode(codeField.getText());
        data.setDebitOrCredit(creditField.isSelected() ? DebitCredit.CREDIT.name() : DebitCredit.DEBIT.name());
        data.setSummaryTemplate(summaryTemplateField.getText());
        return data;
    }

    private static final URL FXML_URL = DatasetItemEditController.class.getResource("entryTemplate-edit.fxml");
    private static final String TITLE = bundle.getString("entryTemplate.dialog.title");

    public static void showAddDialog(VoucherEntryTemplateVo dataSetVo, Consumer<VoucherEntryTemplateVo> consumer) throws IOException {

        String title = bundle.getString("dialog.title.prefix.add") + TITLE;

        new EntryTemplateEditController().showDialog(FXML_URL, title, dataSetVo, consumer);
    }

    public static void showEditDialog(VoucherEntryTemplateVo dataSetVo, Consumer<VoucherEntryTemplateVo> consumer) throws IOException {

        String title = bundle.getString("dialog.title.prefix.edit") + TITLE;

        new EntryTemplateEditController().showDialog(FXML_URL, title, dataSetVo, consumer);
    }
}
